import { HttpParams } from '@angular/common/http';

import { BaseEntity, Pagable } from '@core/base';

/** 配置 */
export class Config extends BaseEntity {
  className!: string;
  methodName!: string;
  methodParams!: string;
  cronExpression!: string;
  remark!: string;
  enabled!: boolean;
  state!: string;
  stateName!: string;

  constructor(data?: Partial<Config>) {
    super(data);
    Object.assign(this, data);
  }
}

/** 日志 */
export class Log extends BaseEntity {
  configId!: string;
  className!: string;
  methodName!: string;
  methodParams!: string;
  cronExpression!: string;

  createdDate!: Date;
  configSnapshot!: string;
  state!: string;
  message!: string;
  costTime!: number;

  constructor(data?: Partial<Log>) {
    super(data);
    Object.assign(this, data);

    if (data?.createdDate != undefined) {
      this.createdDate = new Date(data.createdDate);
    }
  }
}

/** 任务 */
export class Task {
  startUpTime!: Date;
  lastRunTime!: Date;
  numOfRuns!: number;
  numOfErrors!: number;
  state!: string;
  message!: string;
  costTime!: number;
  scheduled!: boolean;

  configId!: string;
  className!: string;
  methodName!: string;
  methodParams!: string;
  cronExpression!: string;
  remark!: string;
  enabled!: string;

  constructor(data?: Partial<Task>) {
    Object.assign(this, data);

    if (data?.startUpTime != undefined) {
      this.startUpTime = new Date(data.startUpTime);
    }

    if (data?.lastRunTime != undefined) {
      this.lastRunTime = new Date(data.lastRunTime);
    }
  }
}

/** 任务查询对象 */
export class TaskQuery {
  className!: string;
  enabled!: boolean;
  states: string[] = [];
  asc!: boolean;

  constructor(data?: Partial<TaskQuery>) {
    Object.assign(this, data);
  }
}

/** 配置查询对象 */
export class ConfigQuery extends Pagable {
  className!: string;
  enabled!: boolean;

  constructor(data?: Partial<ConfigQuery>) {
    super();
    this.sort.push({
      direction: 'desc',
      property: 'id'
    });
    Object.assign(this, data);
  }

  public override get httpParams(): HttpParams {
    let rslt = super.httpParams;

    if (this.className) {
      rslt = rslt.set('className', this.className);
    }

    if (this.enabled != null) {
      rslt = rslt.set('enabled', this.enabled);
    }
    return rslt;
  }
}

/** 日志查询对象 */
export class LogQuery extends Pagable {
  configId!: string;
  states: string[] = [];

  constructor(data?: Partial<LogQuery>) {
    super();
    this.sort.push({
      direction: 'desc',
      property: 'createdDate'
    });
    Object.assign(this, data);
  }

  public override get httpParams(): HttpParams {
    let rslt = super.httpParams;

    if (this.configId) {
      rslt = rslt.set('configId', this.configId);
    }

    if (this.states !== null) {
      rslt = rslt.set('states', `${this.states}`);
    }
    return rslt;
  }
}
